package XML;

import java.util.HashMap;
import java.util.Iterator;

/**
 * XML标签节点类
 * 
 * @author Sky.Wind
 * 
 */

public class XMLNode {
	public static String replaceLineChar(String str) {
		return str.replace("\r\n", "&rn;").replace("\r", "&r;").replace("\n", "&n;");
	}
	public static String unReplaceLineChar(String str) {
		return str.replace("&rn;", "\r\n").replace("&r;", "\r").replace("&n;", "\n");
	}
	private boolean leafage = false;// 默认不是叶子标签
	private HashMap<String, Object> hm = new HashMap<String, Object>();// 属性值储存容器
	private XMLNode SUPER;// 父级标签对象，null表示主标签

	public String FLAG;// 标签名

	private String data;// 标签内容

	public XMLNode(String string) {
		this.FLAG = string;
	}

	public XMLNode(String string, boolean b) {
		this.FLAG = string;
		this.setLeafage(b);
	}

	/**
	 * 构造标签对象到指定父标签下方法
	 * 
	 * @param flag
	 */
	public XMLNode(XMLNode Super, String flag) {
		this.setSUPER(Super);// 父标签定向
		this.FLAG = flag;// 设置标签名字
	}

	/**
	 * 克隆标签
	 */
	@Override
	public XMLNode clone() {
		String str = this.toString();
		return XML.parseFlag(null, str.substring(1, str.length() - 1));
	}

	/**
	 * 给对象赋值（属性赋值）
	 * 
	 * @param xf
	 */
	public XMLNode copy(XMLNode xf) {
		leafage = xf.leafage;
		FLAG = xf.FLAG;
		hm = xf.hm;
		SUPER = xf.SUPER;
		data = xf.data;
		hm = xf.hm;
		return this;
	}

	/**
	 * 标签名核对
	 * 
	 * @param f
	 *            待核对标签名
	 * @return
	 */
	public boolean FlagHomology(String f) {
		return f.toUpperCase().equals(FLAG.toUpperCase());
	}

	// /**
	// * 标签修饰内容赋值
	// * @param data
	// */
	// public void setData(String data){
	// this.data = data;
	// }
	// /**
	// * @return 返回标签内容
	// */
	// public String getData(){
	// return data;
	// }

	/**
	 * 属性定向取值
	 * 
	 * @param abt
	 * @return
	 */
	public String get(String attr) {
		if (hm.containsKey(attr)) {
			return String.valueOf(hm.get(attr));
		}
		return null;
	}

	public String getData() {
		return data;
	}

	public String getData(boolean meaning) {
		String dat = data;
		if (dat != null) {
			if (meaning) {
				return dat;
			} else {
				dat = dat.replace("<", "&_lt;");
				dat = dat.replace(">", "&_gt;");
			}
			return dat;
		}
		return "";
	}

	public XMLNode getSUPER() {
		return SUPER;
	}

	public boolean isLeafage() {
		return leafage;
	}

	/**
	 * 更新属性
	 * 
	 * @param key
	 *            不区分大小写
	 * @param value
	 *            null表示删除属性
	 */
	public void put(String key, Object value) {
		if (value == null) {
			hm.remove(key);
			return;
		}

		if (value instanceof String) {
			value = unReplaceAttrFlag((String) value);
		}
		hm.put(key, value);

	}

	private String replaceAttrFlag(String string) {
		if (string == null)
			return "";
		return replaceLineChar(string.replace("\"", "&ff;").replace("'", "&f;"));
	}

	public void setData(String data) {
		data = data.replace("&_lt;", "<");
		data = data.replace("&_gt;", ">");
		this.data = data;
	}

	public void setLeafage(boolean leafage) {
		this.leafage = leafage;
	}

	public void setSUPER(XMLNode sUPER) {
		SUPER = sUPER;
	}

	public String take(String key) {
		return String.valueOf(hm.remove(key));
	}

	@Override
	public String toString() {
		StringBuffer str = null;
		if (FLAG.equals("!")) {
			str = new StringBuffer("<!-- " + this.getData(true) + " -->");
		} else {
			String attribute = null;
			str = new StringBuffer("<" + FLAG);
			Iterator<String> ite = hm.keySet().iterator();
			while (ite.hasNext()) {
				attribute = ite.next();
				str.append(" " + attribute + "=\"").append(replaceAttrFlag(get(attribute))).append("\"");
			}
			str.append(leafage ? " />" : ">");
			if (!leafage) {
				str.append(this.getData(false));
				str.append("</" + FLAG + ">");
			}

		}
		return str.toString();
	}

	private String unReplaceAttrFlag(String string) {
		if (string == null)
			return "";
		return unReplaceLineChar(string.replace("&ff;", "\"").replace("&f;", "'"));
	}

}